package com.devicemantra.coordinate.ds.tests;
import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.devicemantra.coordinate.exceptions.operations.CDepartmentDoesNotExistsException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;

@SuppressWarnings("serial")
public class AddFrame extends HttpServlet 
{
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		//resp.setContentType("text/html");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		/* Check whether a department exists with the same name */
		Double lLat =  Double.parseDouble(req.getParameter("lat"));
		Double lLong = Double.parseDouble(req.getParameter("lon"));
		String lDeptName = (String) req.getParameter("dept_name");
		String lEmirateName = (String) req.getParameter("emirate");
		String lAddress = (String) req.getParameter("address");
		String lFrameDesc = req.getParameter("frame_desc");
		
		System.out.println(" New Frame with Lat = " + lLat + 
				" Longitude = " + lLong + 
				"Address = " + lAddress + 
				" Department Name " + lDeptName + 
				" Emirate " + lEmirateName  + " Added !");
		CDepartment lDept = null;
		try {
			lDept = CDepartmentFactory.getDepartment(lDeptName);
		} catch (CDepartmentDoesNotExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		CPoint lNewLoc = new CPoint(lLat,lLong);
		CDepartment lNewDept = lDept;
		//lNewDept = pm.detachCopy(lNewDept);
		CLocation lNewFrame = new CLocation(lNewLoc,lFrameDesc);
		lNewFrame.setLocationDescription(lFrameDesc);
		lNewFrame.setEmirate(lEmirateName);
		lNewFrame.setAddress(lAddress);
		lNewFrame.setLocationId(lNewDept.getLatestLocationId());
		System.out.println("Latest frame Id is " + lNewDept.getLatestLocationId());
		lNewDept.addLocation(lNewFrame);
		pm.makePersistent(lNewDept);
		pm.close();
		
		/*pm = PMF.get().getPersistenceManager();
		javax.jdo.Query q = pm.newQuery("select mLocationKey from " + CLocation.class.getName());
	    List ids = (List) q.execute();
	    resp.getOutputStream().print("Size of list is " + ids.size());
	    Iterator<Key> lIterKeys = ids.iterator();
	    while( lIterKeys.hasNext())
	    {
	    	Key lKey = (Key) lIterKeys.next();
	    	Key lParentKey = lKey.getParent();
	    	//resp.getOutputStream().print("<BR> Child is " + KeyFactory.keyToString(lKey));
	    	//resp.getOutputStream().print(" for parent - " + KeyFactory.keyToString(lParentKey) + "<BR>");
	    	
	    	CLocation lLoc = pm.getObjectById(CLocation.class,lKey);
	    	CFrame lFrame = pm.getObjectById(CFrame.class,lParentKey);
	    	lFrame = pm.detachCopy(lFrame);
	    	//resp.getOutputStream().println("Frame in " + lFrame.getEmirate() + "<BR>  (" + lLoc.getLatitude() + "," + lLoc.getLongitude() + ") "+  "<BR> Owned by "   + lFrame.getOwningDepartment()  + "<BR> Description :" + lFrame.getFrameDescription() + "<BR> Address : " + lFrame.getAddress() + "<BR><BR>");
	    }
	    pm.close(); */
		resp.getOutputStream().println(lNewDept.getLatestLocationId()); 
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
	{
		doPost(req,resp);
	}
	
}
